{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from xml.etree.ElementTree import parse\n",
    "import sys\n",
    "import re\n",
    "from xml.etree.ElementTree import Element, dump\n",
    "import xml.etree.ElementTree as ET\n",
    "\n",
    "\n",
    "\n",
    "file_list=os.listdir(\"pill_label_xml/\")\n",
    "file_list.sort()\n",
    "text_list=os.listdir(\"text_label_xml/\")\n",
    "for file_name in file_list:\n",
    "    if file_name.endswith(\"xml\") is not True:\n",
    "        continue\n",
    "    doc = ET.parse(\"pill_label_xml/\"+file_name)\n",
    "    root = doc.getroot()\n",
    "    object_tag = root.findall(\"object\") #pill\n",
    "    \n",
    "    doc_t = ET.parse(\"text_label_xml/\"+file_name)\n",
    "    root_t = doc_t.getroot()\n",
    "    object_tag_t = root_t.findall(\"object\")\n",
    "    \n",
    "    ###############\n",
    "    \n",
    "    size=root_t.find(\"size\")\n",
    "    \n",
    "    obj=object_tag[0]\n",
    "    box_tag_pill = obj.find(\"bndbox\")\n",
    "    xmin_pill = box_tag_pill.findtext(\"xmin\")\n",
    "    ymin_pill = box_tag_pill.findtext(\"ymin\")\n",
    "    xmax_pill = box_tag_pill.findtext(\"xmax\")\n",
    "    ymax_pill = box_tag_pill.findtext(\"ymax\")\n",
    "    print(\"=\"*50)\n",
    "    print(file_name)\n",
    "    print(xmin_pill, ymin_pill, xmax_pill,ymax_pill) \n",
    "    \n",
    "    remove_weight=size.find(\"width\")\n",
    "    size.remove(remove_weight)\n",
    "        \n",
    "    remove_height=size.find(\"height\")\n",
    "    size.remove(remove_height)\n",
    "    \n",
    "    ET.SubElement(size, \"width\").text = str(int(xmax_pill)-int(xmin_pill))\n",
    "    ET.SubElement(size, \"height\").text = str(int(ymax_pill)-int(ymin_pill))\n",
    "    ET.dump(size)\n",
    "    #print(object_tag_t)\n",
    "    \n",
    "    for obj_t in object_tag_t:\n",
    "        \n",
    "        \n",
    "        \n",
    "        box_tag_text = obj_t.find(\"bndbox\")\n",
    "        xmin_text = box_tag_text.findtext(\"xmin\")\n",
    "        ymin_text = box_tag_text.findtext(\"ymin\")\n",
    "        xmax_text = box_tag_text.findtext(\"xmax\")\n",
    "        ymax_text = box_tag_text.findtext(\"ymax\")\n",
    "        print(xmin_text, ymin_text, xmax_text,ymax_text) #####\n",
    "        \n",
    "        start_point_x=int(xmin_text)-int(xmin_pill)\n",
    "        start_point_y=int(ymin_text)-int(ymin_pill)\n",
    "        \n",
    "        xx= int(xmax_text)-int(xmin_text)\n",
    "        yy= int(ymax_text)-int(ymin_text)\n",
    "        \n",
    "        result_xmin=start_point_x\n",
    "        result_xmax=start_point_x+xx\n",
    "        result_ymin=start_point_y\n",
    "        result_ymax=start_point_y+yy\n",
    "        \n",
    "        print(result_xmin,result_xmax,result_ymin,result_ymax)\n",
    "        \n",
    "        bndbox=obj_t.find(\"bndbox\")\n",
    "        \n",
    "        remove_xmin=box_tag_text.find(\"xmin\")\n",
    "        bndbox.remove(remove_xmin)\n",
    "        \n",
    "        remove_ymin=box_tag_text.find(\"ymin\")\n",
    "        bndbox.remove(remove_ymin)\n",
    "        \n",
    "        remove_xmax=box_tag_text.find(\"xmax\")\n",
    "        bndbox.remove(remove_xmax)\n",
    "        \n",
    "        remove_ymax=box_tag_text.find(\"ymax\")\n",
    "        bndbox.remove(remove_ymax)\n",
    "        \n",
    "        ET.SubElement(bndbox, \"xmin\").text = str(result_xmin)\n",
    "        ET.SubElement(bndbox, \"xmax\").text = str(result_xmax)\n",
    "        ET.SubElement(bndbox, \"ymin\").text = str(result_ymin)\n",
    "        ET.SubElement(bndbox, \"ymax\").text = str(result_ymax)\n",
    "        ET.dump(bndbox)\n",
    "        doc_t.write('result/'+file_name)\n",
    "        \n",
    "        \n",
    "     \n",
    "        \n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
